package org.example.dfs;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liyishan
 * @date 2023/8/30 10:22
 * @apiNote
 */

public class Test00022GenerateParenthesis {

    public List<String> generateParenthesis(int n) {
        List<String> ans = new ArrayList<String>();
        dfs(ans, new StringBuilder(), 0, 0, n);
        return ans;
    }

    public static void dfs(List<String> ans,StringBuilder cur,int open,int close,int max){
        if(cur.length() == max*2){
            ans.add(cur.toString());
            return;
        }
        if(open<max){
           cur.append('(');
           dfs(ans,cur,open+1,close,max);
           cur.deleteCharAt(cur.length()-1);
        }
        if(close <open){
            cur.append(')');
            dfs(ans,cur,open,close+1,max);
            cur.deleteCharAt(cur.length()-1);
        }
    }
}
